package com.platform.modules.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.platform.common.annotation.SysLog;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.RestResponse;
import com.platform.common.validator.ValidatorUtils;
import com.platform.common.validator.group.AddGroup;
import com.platform.common.validator.group.UpdateGroup;
import com.platform.modules.sys.entity.SysDictEntity;
import com.platform.modules.sys.entity.SysDictGroupEntity;
import com.platform.modules.sys.service.SysDictGroupService;
import com.platform.modules.sys.service.SysDictService;

/**
 * 数据字典分组Controller
 *
 * @author 肖亮
 */
@Api(tags = "SysDictGroupController|数据字典分组管理")
@RestController
@RequestMapping("sys/dictgroup")
public class SysDictGroupController {
	@Autowired
	private SysDictGroupService sysDictGroupService;
	@Autowired
	private SysDictService sysDictService;

	/**
	 * 查看所有列表
	 *
	 * @param params
	 *            查询参数
	 * @return RestResponse
	 */
	@ApiOperation(value = "查看数据字典分组列表", notes = "查看数据字典分组列表")
	  @ApiImplicitParams({
      @ApiImplicitParam(paramType = "query", name = "code", value = "分组编码", dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "name", value = "分组名称", dataType = "String")
     })
	@PostMapping("/queryAll")
	@RequiresPermissions("sys:dictgroup:list")
	public RestResponse queryAll(String code,String name) {

		  Map<String,Object> params=new HashMap<String,Object>();
			  params.put("code", code);
			  params.put("name", name);
		List<SysDictGroupEntity> list = sysDictGroupService.queryAll(params);

		return RestResponse.success().put("list", list);
	}
	
	/**
	 * 查看数据字典分组以及分组下字典（树形结构）
	 * wangLc
	 * @param params
	 * @return RestResponse
	 */
	@ApiOperation(value = "查看数据字典分组以及分组下字典（树形结构）", notes = "查看数据字典分组以及分组下字典（树形结构）")
	@PostMapping("/selectQuery")
	public RestResponse selectQuery() {
		  Map<String,Object> params=new HashMap<String,Object>();
		List<SysDictGroupEntity> list = sysDictGroupService.queryAll(params);
        if(list != null && list.size()>0){
        	for(SysDictGroupEntity sysDictGroup:list){
        		Map<String,Object> dictParams=new HashMap<String,Object>();
        		String groupId = sysDictGroup.getId();
        		dictParams.put("groupId",groupId);
        		List<SysDictEntity> children = sysDictService.queryList(dictParams);
        		if(children !=null && children.size()>0){
        			sysDictGroup.setChildren(children);
        		}
        	}
        }
		return RestResponse.success().put("list", list);
	}

	/**
	 * 分页查询
	 *
	 * @param params
	 *            查询参数
	 * @return RestResponse
	 */
	@ApiOperation(value = "数据字典分组列表", notes = "根据条件获取所有的数据字典分组")
	  @ApiImplicitParams({
      @ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
      @ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
      @ApiImplicitParam(paramType = "query", name = "code", value = "分组编码", dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "name", value = "分组名称", dataType = "String")
    })
	@PostMapping("/list")
	@RequiresPermissions("sys:dictgroup:list")
	public RestResponse list(Integer currPage,Integer limit,String code,String name) {
		Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			params.put("code", code);
			params.put("name", name);
			IPage page = sysDictGroupService.queryPage(params);

		return RestResponse.success().put("page", page);
	}
	


	/**
	 * 根据主键查询详情
	 *
	 * @param id  
	 *            主键
	 * @return RestResponse
	 */
	@ApiOperation(value = "数据字典分组列表", notes = "根据条件获取所有的数据字典分组")
	  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "id", value = "主键", dataType = "String")
    })
//	@RequestMapping("/info/{id}")
	@PostMapping("/info")
	@RequiresPermissions("sys:dictgroup:info")
	public RestResponse info(String id) {
		SysDictGroupEntity sysDictGroup = sysDictGroupService.getById(id);

		return RestResponse.success().put("dictgroup", sysDictGroup);
	}

	/**
	 * 保存数据字典分组
	 *
	 * @param sysDictGroup
	 *            sysDictGroup
	 * @return RestResponse
	 */
	@ApiOperation(value = "数据字典分组列表", notes = "根据条件获取所有的数据字典分组")
	  @ApiImplicitParams({
		  
    })
	@SysLog("保存数据字典分组")
	@PostMapping("/save")
	@RequiresPermissions("sys:dictgroup:save")
	public RestResponse save(SysDictGroupEntity sysDictGroup) {
		ValidatorUtils.validateEntity(sysDictGroup, AddGroup.class);
		sysDictGroupService.add(sysDictGroup);

		return RestResponse.success();
	}

	/**
	 * 修改数据字典分组
	 *
	 * @param sysDictGroup
	 *            sysDictGroup
	 * @return RestResponse
	 */
	@ApiOperation(value = "修改数据字典分组", notes = "修改数据字典分组")
	@SysLog("修改数据字典分组")
	@PostMapping("/update")
	@RequiresPermissions("sys:dictgroup:update")
	public RestResponse update(SysDictGroupEntity sysDictGroup) {
		ValidatorUtils.validateEntity(sysDictGroup, UpdateGroup.class);
		sysDictGroupService.update(sysDictGroup);

		return RestResponse.success();
	}

	/**
	 * 删除数据字典分组
	 *
	 * @param ids
	 *            ids
	 * @return RestResponse
	 */
	@ApiOperation(value = "删除数据字典分组", notes = "根据id删除模型（支持多id删除）")
	@SysLog("删除数据字典分组")
	@PostMapping("/delete")
	@RequiresPermissions("sys:dictgroup:delete")
	public RestResponse delete(String []ids) {
		
         if(ids != null){
        	 sysDictGroupService.deleteBatch(ids);
         }
		return RestResponse.success();
	}
}
